home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / tkern10.zip / GNUISH\PWD.C < prev    next >
C/C++ Source or Header  |  1990-09-23  |  4KB  |  206 lines

  1. /*  pwd.c - Try to approximate UN*X's getuser...() functions under MS-DOS.
  2.     Copyright (C) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 1, or (at your option)
  7.     any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.     $Header: e:/gnu/lib/RCS/pwd.c 1.2 90/09/23 11:20:57 tho Exp $
  19. */
  20.  
  21. /* This 'implementation' is conjectured from the use of this functions in
  22.    the RCS and BASH distributions.  Of course these functions don't do too
  23.    much useful things under MS-DOS, but using them avoids many "#ifdef
  24.    MSDOS" in ported UN*X code ...  */
  25.  
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <pwd.h>
  31.  
  32. static char *lookup_env (char **);
  33.  
  34. /* where people might scribble their name into the environment ... */
  35.  
  36. static char *login_strings[] =
  37. {
  38.   "LOGIN", "USER", "MAILNAME", (char *) 0
  39. };
  40.  
  41. static char *group_strings[] =
  42. {
  43.   "GROUP", (char *) 0
  44. };
  45.  
  46.  
  47. static char *anonymous = "anonymous";    /* if all else fails ... */
  48.  
  49. static char *home_dir = ".";    /* we feel (no|every)where at home */
  50. static char *login_shell = "not command.com!";
  51.  
  52. static char *login = (char *) 0;/* cache the names here    */
  53. static char *group = (char *) 0;
  54.  
  55. static struct passwd pw;    /* should we return a malloc()'d structure   */
  56. static struct group gr;        /* instead of pointers to static structures? */
  57.  
  58. /* return something like a username in a (butchered!) passwd structure. */
  59. struct passwd *
  60. getpwuid (int uid)
  61. {
  62.   pw.pw_name = getlogin ();
  63.   pw.pw_dir = home_dir;
  64.   pw.pw_shell = login_shell;
  65.   pw.pw_uid = 0;
  66.  
  67.   return &pw;
  68. }
  69.  
  70. struct passwd *
  71. getpwnam (char *name)
  72. {
  73.   return (struct passwd *) 0;
  74. }
  75.  
  76. /* return something like a groupname in a (butchered!) group structure. */
  77. struct group *
  78. getgrgid (int uid)
  79. {
  80.   gr.gr_name = getgr_name ();
  81.   gr.gr_gid = 0;
  82.  
  83.   return &gr;
  84. }
  85.  
  86. struct group *
  87. getgrnam (char *name)
  88. {
  89.   return (struct group *) 0;
  90. }
  91.  
  92. /* return something like a username. */
  93. char *
  94. getlogin ()
  95. {
  96.   if (!login)            /* have we been called before? */
  97.     login = lookup_env (login_strings);
  98.  
  99.   if (!login)            /* have we been successful? */
  100.     login = anonymous;
  101.  
  102.   return login;
  103. }
  104.  
  105. /* return something like a group.  */
  106. char *
  107. getgr_name ()
  108. {
  109.   if (!group)            /* have we been called before? */
  110.     group = lookup_env (group_strings);
  111.  
  112.   if (!group)            /* have we been successful? */
  113.     group = anonymous;
  114.  
  115.   return group;
  116. }
  117.  
  118. /* return something like a uid.  */
  119. int
  120. getuid ()
  121. {
  122.   return 0;            /* every user is a super user ... */
  123. }
  124.  
  125. int
  126. getgid ()
  127. {
  128.   return 0;
  129. }
  130.  
  131. int
  132. geteuid ()
  133. {
  134.   return 0;
  135. }
  136.  
  137. int
  138. getegid ()
  139. {
  140.   return 0;
  141. }
  142.  
  143. struct passwd *
  144. getpwent ()
  145. {
  146.   return (struct passwd *) 0;
  147. }
  148.  
  149. void
  150. setpwent ()
  151. {
  152. }
  153.  
  154. void
  155. endpwent ()
  156. {
  157. }
  158.  
  159. void
  160. endgrent ()
  161. {
  162. }
  163.  
  164. /* return groups.  */
  165. int
  166. getgroups (int ngroups, int *groups)
  167. {
  168.   *groups = 0;
  169.   return 1;
  170. }
  171.  
  172. /* lookup environment.  */
  173. static char *
  174. lookup_env (char *table[])
  175. {
  176.   char *ptr;
  177.   char *entry;
  178.   size_t len;
  179.  
  180.   while (*table && !(ptr = getenv (*table++))) ;    /* scan table */
  181.  
  182.   if (!ptr)
  183.     return (char *) 0;
  184.  
  185.   len = strcspn (ptr, " \n\t\n\r");    /* any WS?       */
  186.   if (!(entry = malloc (len + 1)))
  187.     {
  188.       fprintf (stderr, "Out of memory.\nStop.");
  189.       exit (-1);
  190.     }
  191.  
  192.   strncpy (entry, ptr, len);
  193.   entry[len] = '\0';
  194.  
  195.   return entry;
  196.  
  197. }
  198.  
  199. /*
  200.  * Local Variables:
  201.  * mode:C
  202.  * ChangeLog:ChangeLog
  203.  * compile-command:make
  204.  * End:
  205.  */
  206.